/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { beforeAll, describe, expect, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from './common/testCommon';
import path from 'path';
import { Rule } from '../../../src/Index';
import { CheckEntry } from '../../../src/utils/common/CheckEntry';
import { StackComponentNestingCheck } from '../../../src/checker/performance/StackComponentNestingCheck';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    const rule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/StackComponentNesting', rule, CHECK_MODE.PROJECT2CHECK, StackComponentNestingCheck);
    realPath = checkEntry.scene.getRealProjectDir();
})

describe('StackComponentNestingCheckTest', () => {
    test('StackComponentNestingCheckTest_001_Report', () => {
        const detectFile: string = path.join(realPath, 'ets', 'StackComponentNestingReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(1);
        
        // 验证告警内容
        const report = detectFileReports[0];
        expect(report.defect.description).toContain('Avoid using Stack component with exactly 2 children (Image and Text) or multiple children containing both Image and Text');
        
        // 打印实际的行列号用于调试
        console.log(`Actual line: ${report.defect.reportLine}, column: ${report.defect.reportColumn}, fixKey: ${report.defect.fixKey}`);
        
        // 验证告警的行列号
        expect(report.defect.reportLine).toBe(5);
        expect(report.defect.reportColumn).toBe(5);
        expect(report.defect.fixKey).toBe('5%5%9%');
    });

    test('StackComponentNestingCheckTest_002_NoReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'StackComponentNestingNoReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });


    test('StackComponentNestingCheckTest_003_Multiple', () => {
        const detectFile: string = path.join(realPath, 'ets', 'StackComponentNestingMultiple.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        // 应该有3个 Stack 告警（第1个、第2个、第3个）
        expect(detectFileReports.length).toBe(3);
        
        // 打印实际的行列号用于调试
        detectFileReports.forEach((report, index) => {
            console.log(`Multiple report ${index}: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        });
        
        // 验证告警内容和行列号
        const expectedLines = [7, 18, 31];
        const expectedColumns = [7, 7, 7];
        const expectedFixKeys = ['7%7%11%', '18%7%11%', '31%7%11%'];
        
        detectFileReports.forEach((report, index) => {
            expect(report.defect.description).toContain('Avoid using Stack component with exactly 2 children (Image and Text) or multiple children containing both Image and Text');
            expect(report.defect.reportLine).toBe(expectedLines[index]);
            expect(report.defect.reportColumn).toBe(expectedColumns[index]);
            expect(report.defect.fixKey).toBe(expectedFixKeys[index]);
        });
    });
})
